local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local playerdatadao = require "playerdatadao"
local timetool = require "timetool"
local msgproxy = require "msgproxy"
local processstate = require "processstate"
local table = table
require "enum"
local processing = processstate:new({timeout=4})
local  GiveGemstone = {}

--[[
//请求赠送房卡操作
message GiveGemstoneReq {
	optional Version version = 1;
	optional int32 from_rid = 2;
	optional int32 to_rid = 3;
	optional int32 gemstone = 4;
	optional string password = 5;	
}

//响应赠送房卡操作
message GiveGemstoneRes {
	optional int32 errcode = 1;
	optional string errcodedes = 2;	
}
]]

function  GiveGemstone.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}
	local server = msghelper:get_server()

	--检查当前登陆状态
	if not msghelper:is_login_success() then
		filelog.sys_error("GiveGemstone.process invalid server state", server.state)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求!"
		msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)		
		return
	end

	if processing:is_processing() then
        responsemsg.errcode = EErrCode.ERR_DEADING_LASTREQ
        responsemsg.errcodedes = "正在处理上一次请求！"
        msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)
        return
    end


	if request.from_rid == nil or request.to_rid == nil 
		or request.gemstone == nil or  request.password == nil
		or request.from_rid ~= server.rid
		or request.gemstone <= 0 then
		filelog.sys_error("GiveGemstone.process invalid params")
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求参数!"
		msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)		
		return		
	end

	if not playerdatadao.is_player_exist(request.to_rid) then
		filelog.sys_error("GiveGemstone.process to_player not exist", request)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的目标玩家rid!"
		msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)		
		return		
	end

	if not msghelper:is_login_success() then
		filelog.sys_error("GiveGemstone.process invalid server state", server.state)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求!"
		msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)		
		return
	end

	if  server.info.agentlevel == nil or server.info.agentlevel <= 0 then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "你当前不是代理，不能执行该操作!"
		msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)		
		return		
	end

	if server.money.password ~= msghelper:generate_zhuanzhang_password(request.password) then
		responsemsg.errcode = EErrCode.ERR_INVALID_PASSWORD
		responsemsg.errcodedes = "无效的密码!"
		msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)		
		return
	end 

	if server.money.gemstone < request.gemstone then
		responsemsg.errcode = EErrCode.ERR_NOT_ENOUGH_GEMSTONE
		responsemsg.errcodedes = "您当前的房卡余额不足!"
		msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)		
		return
	end

    processing:set_process_state(true)
	local _, to_playerinfo = playerdatadao.query_player_info(request.to_rid)
	local _, to_playermoney = playerdatadao.query_player_money(request.to_rid)
	local _, to_playeronline = playerdatadao.query_player_online(request.to_rid)
	if not msghelper:is_login_success() then
		filelog.sys_error("GiveGemstone.process invalid server state", server.state)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求!"
		msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)		
    	processing:set_process_state(false)
		return
	end

	msghelper:save_player_gemstone(server.rid, -(request.gemstone), EReasonChangeCurrency.CHANGE_CURRENCY_GIVE, "givegemstone")

	msghelper:send_gemstonechange_notice()

	local giftrecorditem = {
	    from_rid = request.from_rid,
	    from_rolename = server.info.rolename,
	    from_logo = server.info.logo,
	    to_rid = request.to_rid,
	    to_rolename = to_playerinfo.rolename,
	    to_logo = to_playerinfo.logo,
	    gemstone = request.gemstone,
	    create_time = timetool.get_time(),
	}

	playerdatadao.save_player_giftrecords("insert", server.rid, giftrecorditem, nil)

	if to_playeronline.gatesvr_id == nil 
		or to_playeronline.gatesvr_id == "" then
		msghelper:save_player_gemstone(request.to_rid, request.gemstone, EReasonChangeCurrency.CHANGE_CURRENCY_GIVE, "givegemstone", to_playermoney)
	else
		local status = msgproxy.sendrpc_syncnoticemsgto_gatesvrd(to_playeronline.gatesvr_id, to_playeronline.gatesvr_service_address, "givegemstone", request)
		if not status then
			msghelper:save_player_gemstone(request.to_rid, request.gemstone, EReasonChangeCurrency.CHANGE_CURRENCY_GIVE, "givegemstone", to_playermoney)
		end
	end
	processing:set_process_state(false)

	msghelper:send_resmsgto_client(fd, "GiveGemstoneRes", responsemsg)
end

return GiveGemstone

